package com.incar.chuxing.widget;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.incar.chuxing.R;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by user1 on 2017/7/25.
 */

public class MyTimePicker {
    private OptionsPickerView pvOptions;
    private Map<String, String> totalDaysMap = new LinkedHashMap<>();
    ArrayList<String> totalDaysKeyList = new ArrayList<>();
    private ArrayList<ArrayList<String>> totalTimeList = new ArrayList<>();
    private Context context;
    String[] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
    private int days;
    private long maxTime;

    public MyTimePicker(Context context, int days, long minTime, long maxTime) {
        this.context = context;
        this.days = days;
        this.maxTime = maxTime;
        init(minTime);
    }

    private OnTimeChosenListener mOnCountChangeListener;

    public interface OnTimeChosenListener {
        void onTimeChosen(long timeMills);
    }

    public void setOnTimeChosenListener(OnTimeChosenListener onTimeChosenListener) {
        mOnCountChangeListener = onTimeChosenListener;
    }

    private void init(long minTime) {
        //选项选择器
        pvOptions = new OptionsPickerBuilder(context, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                String time = totalDaysMap.get(totalDaysKeyList.get(options1)) + " " + totalTimeList.get(options1).get(options2);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
                try {
                    mOnCountChangeListener.onTimeChosen(sdf.parse(time).getTime());
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        })
                .setLayoutRes(R.layout.pickerview_custom_options, new CustomListener() {
                    @Override
                    public void customLayout(View v) {
                        final TextView tv_cancel = (TextView) v.findViewById(R.id.tv_cancel);
                        final TextView tv_sure = (TextView) v.findViewById(R.id.tv_sure);
                        tv_cancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvOptions.dismiss();
                            }
                        });

                        tv_sure.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvOptions.returnData();
                                pvOptions.dismiss();
                            }
                        });
                    }
                })
                .setCyclic(false, false, true)
                .setContentTextSize(19)
                .setTitleText("用车时间")
                .setTitleSize(20)
                .build();
        initData(days, minTime);

        for (String key : totalDaysMap.keySet()) {
            totalDaysKeyList.add(key);
        }
        pvOptions.setPicker(totalDaysKeyList, totalTimeList);

    }

    public void showPicker() {
        if (pvOptions != null) {
            pvOptions.show();
        }

    }

    /**
     * 获得未来几天的数据
     *
     * @param totalDays 要获取的天数
     * @return Map的键名是了滚轴要显示的数据，键值是实际代表的日期
     */
    private void initData(int totalDays, long minTime) {
        DateFormat dfTime = new SimpleDateFormat("HH:mm");
        DateFormat dfKey = new SimpleDateFormat("MM月dd日");
        DateFormat dfValue = new SimpleDateFormat("yyyy年MM月dd日");
        String dayKey;
        String dayValue;
        String week;
        Calendar calFrom = Calendar.getInstance();
        calFrom.setTimeInMillis(minTime);
        String nowDate = dfValue.format(calFrom.getTime());
        int minute = calFrom.get(Calendar.MINUTE);
        int lastDay = -1;
        int newDay;
        if (minute != 0 && minute != 30) {
            calFrom.add(Calendar.MINUTE, 30 - minute % 30);
        }
        ArrayList<String> timeList = new ArrayList<>();
        for (int i = 0; i < totalDays * 24 * 2; i++) {
            newDay = calFrom.get(Calendar.DAY_OF_MONTH);
            if (newDay != lastDay) {
                lastDay = newDay;
                dayKey = dfKey.format(calFrom.getTime());
                dayValue = dfValue.format(calFrom.getTime());
                int dayInterval = daysBetween(nowDate, dayValue);
                week = weekDays[(calFrom.get(Calendar.DAY_OF_WEEK) - 1) % 7];
                if (dayInterval == 0) {
                    week = "今天";
                }
                totalDaysMap.put(dayKey + " " + week, dayValue);
                timeList = new ArrayList<>();
                totalTimeList.add(timeList);//如果是新的一天，将前一天的时间数据添加到总的list中
            }
            timeList.add(dfTime.format(calFrom.getTime()));  //将时间加入到list中
            calFrom.add(Calendar.MINUTE, 30);
            if (calFrom.getTimeInMillis() > maxTime) return;

        }
/*
        Log.i("timeString", totalDaysMap.toString());
        Log.i("timeString", totalTimeList.toString());*/
    }

    private int daysBetween(String smdate, String bdate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime(sdf.parse(smdate));
            long time1 = cal.getTimeInMillis();
            cal.setTime(sdf.parse(bdate));
            long time2 = cal.getTimeInMillis();
            long between_days = (time2 - time1) / (1000 * 3600 * 24);
            return Integer.parseInt(String.valueOf(between_days));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
